package cbh.tool.common;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by hx-pc on 16-9-5.
 */
public class Tool {

    public static String getRandom(int num) {
        String[] strs = {
                "q", "w", "e", "r", "t", "y", "u", "i",
                "o", "p", "a", "s", "d", "f", "g", "h",
                "j", "k", "l", "z", "x", "c", "v", "b",
                "n", "m"
        };
        String str = "";
        for (int i = 0; i < num; i++) {
            str += strs[(int) (1 + Math.random() * (25 - 0 + 0))];
        }
        return str;
    }

    /**
     * 请保证数组第一个map都有值
     * @param linkedHashMaps
     */
    @SuppressWarnings("unchecked")
    public static void clearNull(List<LinkedHashMap> linkedHashMaps) {
        LinkedHashMap validateMap = linkedHashMaps.get(0);
        for (int i = 1 ; i < linkedHashMaps.size(); i ++) {
            LinkedHashMap doLinkedHasMap = linkedHashMaps.get(i);
            doLinkedHasMap.keySet().stream().filter(o -> Objects.isNull(doLinkedHasMap.get(o))).forEach(o -> {
                Object validateObject = validateMap.get(o);
                Object result = new Object();
                if (validateObject instanceof Integer) {
                    result = 0;
                } else if (validateObject instanceof Long) {
                    result = 0;
                } else if (validateObject instanceof Double) {
                    result = 0.0;
                } else if (validateObject instanceof Float) {
                    result = 0.0;
                } else if (validateObject instanceof String) {
                    result = "";
                }
                doLinkedHasMap.put(o, result);
            });
        }
    }

}
